********************************************************************
*Table A16: Robots and college enrollment at state level Enrollment*
********************************************************************

*************************************************
* 		Columns 1-3: outcome at CZ level 		*
*************************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_automation/states_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports merge
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate //beginning of period
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

*IPEDS
*merge 1:1 statefip year using "${final_data}IPEDS_EnrollmentState.dta", keep(3) nogenerate

xtset czone year

*only first two periods
keep if year<=2000

*Standardization
foreach var in expof_us_adj expof_euro7_qo {
qui su `var'
replace `var' = (`var'-`r(mean)')/`r(sd)'
}

foreach var in public communityCollege uniTop20 uniTop30Dummy uniTop30 largest150 forProfit nonProfit numberOfUni private {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)
	
/* save data for Carrillo and Iglesias	
preserve
keep dEnr_und4Y dEnr_und2Y division year statefip ipums_pop_1990 region d_sc_70_90 expof_us_adj d_exp_us_stacked expof_euro7_qo d_exp_iv_stacked expof_state_us_adj d_exp_us_stacked  expof_state_euro7_qo d_exp_iv_stacked expof_state_noCZ_us_adj d_exp_us_stacked expof_state_noCZ_euro7_qo d_exp_iv_stacked $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990
save "/Users/giuseppedigiacomo/Desktop/data_tableA16_col_1_3.dta", replace 
restore */

eststo clear
*Column 1: CZ enrollment on CZ exposure
eststo pAc1: ivreghdfe dEnr_und4Y (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

eststo pBc1: ivreghdfe dEnr_und2Y (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

*Column 2: CZ enrollment on state exposure
eststo pAc2: ivreghdfe dEnr_und4Y (expof_state_us_adj d_exp_us_stacked = expof_state_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

eststo pBc2: ivreghdfe dEnr_und2Y (expof_state_us_adj d_exp_us_stacked = expof_state_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

*Column 3: CZ enrollment on state exposure (excluding CZ)
eststo pAc3: ivreghdfe dEnr_und4Y (expof_state_noCZ_us_adj d_exp_us_stacked = expof_state_noCZ_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

eststo pBc3: ivreghdfe dEnr_und2Y (expof_state_noCZ_us_adj d_exp_us_stacked = expof_state_noCZ_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)


*************************************************
* 		Column 4: outcome at state level 		*
*************************************************

*Education
use "$clean_data_lmarket/census_1990_populationDataState.dta", clear //constant for weights
merge 1:m statefip using "$final_data_outcomes/IPEDS_EnrollmentState.dta", assert(1 3) keep(3) nogenerate

*Automation
merge 1:1 statefip year using "$final_data_automation/states_ExposureAutomationAggregated.dta", keep(3) nogenerate

*Imports merge
merge 1:1 statefip year using "$final_data_imports/state_ExposureChina", keep(3) nogenerate

xtset statefip year

*Keep only periods of interest
keep if year<=2000

*Standardization
foreach var in expof_state_us_adj expof_state_euro7_qo d_exp_state_us_stacked d_exp_state_iv_stacked { 
qui su `var'
replace `var' = (`var'-`r(mean)')/`r(sd)'
}

rename ipums_pop ipums_pop_1990
/* save data for Carrillo and Iglesias	
preserve
keep dEnr_und4Y dEnr_und2Y division year statefip ipums_pop_1990 region expof_state_us_adj d_exp_state_us_stacked expof_state_euro7_qo d_exp_state_iv_stacked
save "/Users/giuseppedigiacomo/Desktop/data_tableA16_col_4.dta", replace
restore 
*/

*Column 4: State enrollment on state exposure
*eststo pAc4: ivreghdfe dEnr_und4Y (expof_state_us_adj d_exp_state_us_stacked = expof_state_euro7_qo d_exp_state_iv_stacked)  [w=ipums_pop_1990], absorb(division##year) cluster(statefip)

*eststo pBc4: ivreghdfe dEnr_und2Y (expof_state_us_adj d_exp_state_us_stacked = expof_state_euro7_qo d_exp_state_iv_stacked)  [w=ipums_pop_1990], absorb(division##year) cluster(statefip)


*Note: no time-invariant covariates needed since we already control for state fixed effects

*Column 5: State enrollment on state exposure with state FE
eststo pAc5: ivreghdfe dEnr_und4Y (expof_state_us_adj d_exp_state_us_stacked = expof_state_euro7_qo d_exp_state_iv_stacked)  [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

eststo pBc5: ivreghdfe dEnr_und2Y (expof_state_us_adj d_exp_state_us_stacked = expof_state_euro7_qo d_exp_state_iv_stacked)  [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)



estout pAc1 pAc2 pAc3 pAc5 using "$tables_appendix/Table A15a.tex", style(tex) ///
rename(expof_us_adj xVar expof_state_us_adj xVar expof_state_noCZ_us_adj xVar) ///
varlabels(xVar "US robot exposure") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(xVar) ///
order(xVar)

estout pBc1 pBc2 pBc3 pBc5 using "$tables_appendix/Table A15b.tex", style(tex) ///
rename(expof_us_adj xVar expof_euro7_qo xVar expof_state_us_adj xVar expof_state_noCZ_us_adj xVar) ///
varlabels(xVar "US robot exposure") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(xVar) ///
order(xVar)
